package com.xingda.packing.dmss.minitor.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xingda.packing.dmss.domain.CacheLocation;
import com.xingda.packing.dmss.domain.ProductBoxLocation;
import com.xingda.packing.dmss.domain.ShuntProductionLine;
import com.xingda.packing.dmss.http.PackingRequireHttp;
import com.xingda.packing.dmss.http.WheelHttp;
import com.xingda.packing.dmss.led.LedOperateService;
import com.xingda.packing.dmss.minitor.entity.PkBoxLocation;
import com.xingda.packing.dmss.minitor.entity.PkTask;
import com.xingda.packing.dmss.minitor.mapper.PkBoxLocationMapper;
import com.xingda.packing.dmss.minitor.mapper.PkTaskMapper;
import com.xingda.packing.dmss.plc.service.PlcDirectiveService;
import com.xingda.packing.dmss.task.ticket.AbstractTaskTicket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/debug")
@Slf4j
public class DebugController {
    @Resource
    private ShuntProductionLine shuntProductionLine;
    @Resource
    private LedOperateService ledOperateService;
    @Resource
    private PkBoxLocationMapper pkBoxLocationMapper;
    @Resource
    private PlcDirectiveService plcDirectiveService;
    @Autowired
    private PkTaskMapper pkTaskMapper;

    @GetMapping("getProductMap")
    public HashMap<String, Integer> getProductMap(int area) {
        return shuntProductionLine.getPackProductionLine().getProductArea().getProductMap(area);
    }

    @GetMapping("getNoProductMap")
    public HashMap<String, Integer> getNoProductMap(int area) {
        return shuntProductionLine.getPackProductionLine().getProductArea().getNotProductMap(area);
    }

    @GetMapping("getProductList")
    public List<ProductBoxLocation> getProductList(int area) {
        return shuntProductionLine.getPackProductionLine().getProductArea().getLocationList(area);
    }

    @GetMapping("getTaskTicket")
    public AbstractTaskTicket abstractTaskTicket(int area) {
        return shuntProductionLine.getPackProductionLine().getRobot().getTaskTicket(area);
    }

    @GetMapping("getCacheNum")
    public List<String> getCacheNum() {
        List<String> list = new ArrayList<>();
        int maxB80LocationRNo = shuntProductionLine.getPackProductionLine().getCacheArea().getMaxB80LocationRNo();
        int maxB60LocationRNo = shuntProductionLine.getPackProductionLine().getCacheArea().getMaxB60LocationRNo();
        int maxB80LocationLNo = shuntProductionLine.getPackProductionLine().getCacheArea().getMaxB80LocationLNo();
        int maxB60LocationLNo = shuntProductionLine.getPackProductionLine().getCacheArea().getMaxB60LocationLNo();
        list.add("左侧B80最大位置号:" + maxB80LocationLNo);
        list.add("======================================");
        list.add("左侧B40/B60最大位置号:" + maxB60LocationLNo);
        list.add("======================================");
        list.add("右侧B80最大位置号:" + maxB80LocationRNo);
        list.add("======================================");
        list.add("右侧B40/B80最大位置号" + maxB60LocationRNo);
        return list;
    }

    @GetMapping("getWheelInfo")
    public String getWheelInfo(String barCode) {
        return WheelHttp.queryIntermediate(barCode).toJSONString();
    }

    @GetMapping("getTaskInfo")
    public String getTaskInfo(String taskCode) {
        return PackingRequireHttp.searchPackingBoxInspection(taskCode, false, false, false, true).toJSONString();
    }

    @GetMapping("getNextNo")
    public Optional<CacheLocation> nextPutLocationNo(int wheelTypeCode, int area) {
        return shuntProductionLine.getPackProductionLine().getCacheArea().nextPutLocationNo(wheelTypeCode, area);
    }

//    检查led连接是否正常
    @GetMapping("initled")
    public void initLED() {
        log.info("====进入initLED");
        List<PkBoxLocation> pkBoxLocations = pkBoxLocationMapper.selectList(null);
        for (PkBoxLocation pkBoxLocation : pkBoxLocations) {
            ledOperateService.write(pkBoxLocation.getBoxLocationName(), pkBoxLocation.getLedIp(), pkBoxLocation.getLedPort());
        }
    }


//    led赋值
    @GetMapping("valueled")
    public void ledVaule() {

        log.info("====进入ledVaule");

//        存放 装箱号和任务名称 的对应关系
        HashMap<Integer, String> taskMap = new HashMap<>();

        List<PkBoxLocation> pkBoxLocations = pkBoxLocationMapper.selectList(null);
        // 查找end_time为空的记录
        List<PkTask> tasks = pkTaskMapper.selectList(
                new LambdaQueryWrapper<PkTask>()
                        .isNull(PkTask::getEndTime)
                        .select(PkTask::getId,
                                PkTask::getTaskName,
                                PkTask::getSpecifications,
                                PkTask::getBoxLocationId)
        );

//        taskList的个数不是12个报错
        if (tasks.size() != 12) {
            log.warn("==== !!! pk_task表的endTime个数不等于12");
        }

        for (PkTask pkTask : tasks) {
            log.info("==== tasks集合的id:{}, taskName:{}, PkTask::getSpecifications:{}, boxLocationid:{}", pkTask.getId(), pkTask.getTaskName(), pkTask.getSpecifications(), pkTask.getBoxLocationId());
            String msg = pkTask.getTaskName() + "=== 规格 ===" + pkTask.getSpecifications();
            taskMap.put(pkTask.getBoxLocationId(), msg);
        }


        for (PkBoxLocation pkBoxLocation : pkBoxLocations) {
            log.info("==== msg:{}, ip:{}, port:{}", taskMap.get(pkBoxLocation.getId()), pkBoxLocation.getLedIp(), pkBoxLocation.getLedPort());
            ledOperateService.write(taskMap.get(pkBoxLocation.getId()), pkBoxLocation.getLedIp(), pkBoxLocation.getLedPort());
        }
    }

    @GetMapping("agv")
    public void setAgvComing(boolean status) {
        shuntProductionLine.getPackProductionLine().setAgvComing(status);
    }

    @GetMapping("agvStatus")
    public String agvStatus() {
        List<String> agvStatus = new ArrayList<>();
        agvStatus.add("AGV满轮托盘号:" + plcDirectiveService.agvTPH1());
        agvStatus.add("AGV请求取料:" + plcDirectiveService.isPackingStatus());
        agvStatus.add("内收线允许取料:" + plcDirectiveService.allowTask1());
        agvStatus.add("AGV在取料区域:" + plcDirectiveService.isPacking());
        agvStatus.add("AGV取料完成:" + plcDirectiveService.isPackingOk());
        agvStatus.add("AGV请求放托盘:" + plcDirectiveService.isKongTuoStatus());
        agvStatus.add("AGV请求放托盘号:" + plcDirectiveService.agvTPH2());
        agvStatus.add("内收线允许放托盘:" + plcDirectiveService.allowTask2());
        agvStatus.add("AGV在放托盘区域:" + plcDirectiveService.isKongTuo());
        agvStatus.add("AGV放托盘完成:" + plcDirectiveService.isKongTuoOk());
        agvStatus.add("内收线紧急停止:" + plcDirectiveService.isStopSignal());
        agvStatus.add("复位AGV:" + plcDirectiveService.isReset());
        return agvStatus.toString();
    }

    @GetMapping("agvInsert1")
    public void agvInsert(String s1, String s2, int value) {
        plcDirectiveService.agvInsert1(s1, s2, value);
    }

    @GetMapping("agvInsert2")
    public void agvInsert(String s1, String s2, boolean value) {
        plcDirectiveService.agvInsert2(s1, s2, value);
    }
}
